package com.zf.thread_pool;

import com.zf.thread_pool.thread_first.CustomThreadFirstPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * 测试自定义的 线程优先线程池 和 普通线程池
 */
public class CompareNormalAndThreadFirstPool {
    private static final Logger log = LoggerFactory.getLogger(CompareNormalAndThreadFirstPool.class);


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        testNormalThreadPool();
        testThreadFirstPool();
    }

    /**
     * 测试自定义的 线程优先线程池
     */
    public static void testThreadFirstPool() throws ExecutionException, InterruptedException {

        // 创建一个默认参数的线程优先线程池
        // ThreadPoolExecutor executor = CustomThreadFirstPool.getDefaultThreadFirstPool(true);

        // 创建线程优先线程池，定时打印线程池的状态信息
        ThreadPoolExecutor executor = CustomThreadFirstPool.buildThreadFirstPool(
                4,
                16,
                1, TimeUnit.MINUTES,
                1 << 16,
                true
        );

        ArrayList<Future<String>> futureList = new ArrayList<>();
        // submit 提交任务，可以获取返回结果
        int taskNumber = 1000;
        for (int i = 1; i <= taskNumber; i++) {
            int finalI = i;
            Future<String> future = executor.submit(() -> {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("正常的 submit() 的任务{}", finalI);
                return "success " + finalI;
            });
            futureList.add(future);
        }

        // 关闭线程池，将线程池的shutdown标识设置为 true, 不再接收新的任务，继续完成已提交的任务
        // executor.shutdown()不会阻塞当前调用线程
        executor.shutdown();
        log.info("执行完shutdown了");

        // 打印结果，如果任务还没有完成则阻塞获取
        // for (Future<String> future : futureList) {
        //     log.info(future.get());
        // }

        System.out.println("主线程结束了");
    }


    /**
     * 测试普通的线程池
     */
    public static void testNormalThreadPool() {
        String threadPoolName = "我的线程池-1";
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                4, 16,
                1, TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(1 << 16),
                new ThreadPoolExecutor.CallerRunsPolicy());

        // 定时打印线程池状态
        ExecutorManager.printPoolStatusAtFixedRate(executor, threadPoolName, 1000, TimeUnit.MILLISECONDS);

        ArrayList<Future<String>> futureList = new ArrayList<>();
        // submit 提交任务，可以获取返回结果
        int taskNumber = 1000;
        for (int i = 1; i <= taskNumber; i++) {
            int finalI = i;
            Future<String> future = executor.submit(() -> {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("正常的 submit() 的任务{}", finalI);
                return "success " + finalI;
            });
            futureList.add(future);
        }

        // 关闭线程池，将线程池的shutdown标识设置为 true, 不再接收新的任务，继续完成已提交的任务
        // executor.shutdown()不会阻塞当前调用线程
        executor.shutdown();
        System.out.println("执行完shutdown了");

        // 打印结果，如果任务还没有完成则阻塞获取
        // for (Future<String> future : futureList) {
        //     log.info(future.get());
        // }

        System.out.println("主线程结束了");
    }

}
